<!DOCTYPE html>
<html lang="en">

<head><meta name="generator" content="Hexo 3.9.0">
    <meta charset="utf-8">
    
        
                <meta name="keywords" content="爱前端 | 爱工作 | 爱生活">
                
                    
                            <meta name="description" content="This is My blog">
                            
                                <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
                                <title>
                                    
                                        vue 移动端 实现div拖拽移动 |
                                            
                                                一只正在成长的程序猿
                                </title>
                                
                                    <link rel="shortcut icon" href="/images/headPortrait.png">
                                    
                                        <link rel="stylesheet" href="/css/style.css">
                                            
                                                <link rel="stylesheet" href="/fancybox/jquery.fancybox.min.css">
                                                    
                                                        <script src="/js/pace.min.js"></script>
</head></html>

    <body>
        <main class="content">
            <section class="outer">
    <article id="post-vuejs 移动端 实现div拖拽移动" class="article article-type-post" itemscope itemprop="blogPost" data-scroll-reveal>

  <div class="article-inner">
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      vue 移动端 实现div拖拽移动
    </h1>
  
  




      </header>
    

    
      <div class="article-meta">
        <a href="/2019/08/07/vuejs 移动端 实现div拖拽移动/" class="article-date">
  <time datetime="2019-08-07T09:31:46.591Z" itemprop="datePublished">2019-08-07</time>
</a>
        
  <div class="article-category">
    <a class="article-category-link" href="/categories/技术/">技术</a>
  </div>

      </div>
    

    <div class="article-entry" itemprop="articleBody">
      

      

      
        <blockquote>
<p>相关知识点<br>touchstart 当在屏幕上按下手指时触发</p>
<p>touchmove 当在屏幕上移动手指时触发</p>
<p>touchend 当在屏幕上抬起手指时触发<br>mousedown mousemove mouseup对应的是PC端的事件</p>
<p>touchcancel 当一些更高级别的事件发生的时候（如电话接入或者弹出信息）会取消当前的touch操作，即触发touchcancel。一般会在touchcancel时暂停游戏、存档等操作。</p>
</blockquote>
<a id="more"></a>
<h4 id="html"><a href="#html" class="headerlink" title="html"></a>html</h4><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">template</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">"webId"</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">div</span>&gt;</span>你的web页面<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- 如果碰到滑动问题，1.1 请检查这里是否属于同一点。 --&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- 悬浮的HTML --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">div</span> <span class="attr">v-if</span>=<span class="string">"!isShow"</span> <span class="attr">class</span>=<span class="string">"xuanfu"</span> <span class="attr">id</span>=<span class="string">"moveDiv"</span></span></span><br><span class="line"><span class="tag">          @<span class="attr">mousedown</span>=<span class="string">"down"</span> @<span class="attr">touchstart</span>=<span class="string">"down"</span></span></span><br><span class="line"><span class="tag">             //<span class="attr">..prevent</span> 防止滑动时，页面也跟着滑动</span></span><br><span class="line"><span class="tag">          @<span class="attr">mousemove</span>=<span class="string">"move"</span> @<span class="attr">touchmove.prevent</span>=<span class="string">"move"</span>	</span></span><br><span class="line"><span class="tag">          @<span class="attr">mouseup</span>=<span class="string">"end"</span> @<span class="attr">touchend</span>=<span class="string">"end"</span></span></span><br><span class="line"><span class="tag">        &gt;</span></span><br><span class="line">          <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">"yuanqiu"</span>&gt;</span></span><br><span class="line">            &#123;&#123;pageInfo.totalPage&#125;&#125;</span><br><span class="line">          <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h4 id="js"><a href="#js" class="headerlink" title="js"></a>js</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br></pre></td><td class="code"><pre><span class="line">&lt;script&gt;</span><br><span class="line">data() &#123;</span><br><span class="line">  <span class="keyword">return</span> &#123;</span><br><span class="line">    flags: <span class="literal">false</span>,</span><br><span class="line">    position: &#123; <span class="attr">x</span>: <span class="number">0</span>, <span class="attr">y</span>: <span class="number">0</span> &#125;,</span><br><span class="line">    nx: <span class="string">''</span>, <span class="attr">ny</span>: <span class="string">''</span>, <span class="attr">dx</span>: <span class="string">''</span>, <span class="attr">dy</span>: <span class="string">''</span>, <span class="attr">xPum</span>: <span class="string">''</span>, <span class="attr">yPum</span>: <span class="string">''</span>,</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">methods: &#123;</span><br><span class="line">  <span class="comment">// 实现移动端拖拽</span></span><br><span class="line">  down()&#123;</span><br><span class="line">    <span class="keyword">this</span>.flags = <span class="literal">true</span>;</span><br><span class="line">    <span class="keyword">var</span> touch;</span><br><span class="line">    <span class="keyword">if</span>(event.touches)&#123;</span><br><span class="line">        touch = event.touches[<span class="number">0</span>];</span><br><span class="line">    &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">        touch = event;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">this</span>.position.x = touch.clientX;</span><br><span class="line">    <span class="keyword">this</span>.position.y = touch.clientY;</span><br><span class="line">    <span class="keyword">this</span>.dx = moveDiv.offsetLeft;</span><br><span class="line">    <span class="keyword">this</span>.dy = moveDiv.offsetTop;</span><br><span class="line">  &#125;,</span><br><span class="line">  move()&#123;</span><br><span class="line">    <span class="keyword">if</span>(<span class="keyword">this</span>.flags)&#123;</span><br><span class="line">      <span class="keyword">var</span> touch ;</span><br><span class="line">      <span class="keyword">if</span>(event.touches)&#123;</span><br><span class="line">          touch = event.touches[<span class="number">0</span>];</span><br><span class="line">      &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">          touch = event;</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="keyword">this</span>.nx = touch.clientX - <span class="keyword">this</span>.position.x;</span><br><span class="line">      <span class="keyword">this</span>.ny = touch.clientY - <span class="keyword">this</span>.position.y;</span><br><span class="line">      <span class="keyword">this</span>.xPum = <span class="keyword">this</span>.dx+<span class="keyword">this</span>.nx;</span><br><span class="line">      <span class="keyword">this</span>.yPum = <span class="keyword">this</span>.dy+<span class="keyword">this</span>.ny;</span><br><span class="line">      moveDiv.style.left = <span class="keyword">this</span>.xPum+<span class="string">"px"</span>;</span><br><span class="line">      moveDiv.style.top = <span class="keyword">this</span>.yPum +<span class="string">"px"</span>;</span><br><span class="line">      <span class="comment">//阻止页面的滑动默认事件；如果碰到滑动问题，1.2 请注意是否获取到 touchmove</span></span><br><span class="line">      <span class="built_in">document</span>.addEventListener(<span class="string">"touchmove"</span>,<span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">          event.preventDefault();</span><br><span class="line">      &#125;,<span class="literal">false</span>);</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line"><span class="comment">//鼠标释放时候的函数</span></span><br><span class="line">  end()&#123;</span><br><span class="line">    <span class="keyword">this</span>.flags = <span class="literal">false</span>;</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;</span><br><span class="line">&lt;<span class="regexp">/script&gt;</span></span><br></pre></td></tr></table></figure>

<h4 id="css"><a href="#css" class="headerlink" title="css"></a>css</h4><figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">&lt;<span class="selector-tag">style</span>&gt;</span><br><span class="line">  <span class="selector-class">.xuanfu</span> &#123;</span><br><span class="line">    <span class="attribute">height</span>: <span class="number">4.5rem</span>;</span><br><span class="line">    <span class="attribute">width</span>: <span class="number">4.5rem</span>;</span><br><span class="line">    <span class="comment">/* 如果碰到滑动问题，1.3 请检查 z-index。z-index需比web大一级*/</span></span><br><span class="line">    <span class="attribute">z-index</span>: <span class="number">999</span>;</span><br><span class="line">    <span class="attribute">position</span>: fixed;</span><br><span class="line">    <span class="attribute">top</span>: <span class="number">4.2rem</span>;</span><br><span class="line">    <span class="attribute">right</span>: <span class="number">3.2rem</span>;</span><br><span class="line">    <span class="attribute">border-radius</span>: <span class="number">0.8rem</span>;</span><br><span class="line">    <span class="attribute">background-color</span>: <span class="built_in">rgba</span>(0, 0, 0, 0.55);</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="selector-class">.yuanqiu</span> &#123;</span><br><span class="line">    <span class="attribute">height</span>: <span class="number">2.7rem</span>;</span><br><span class="line">    <span class="attribute">width</span>: <span class="number">2.7rem</span>;</span><br><span class="line">    <span class="attribute">border</span>: <span class="number">0.3rem</span> solid <span class="built_in">rgba</span>(140, 136, 136, 0.5);</span><br><span class="line">    <span class="attribute">margin</span>: <span class="number">0.65rem</span> auto;</span><br><span class="line">    <span class="attribute">color</span>: <span class="number">#000000</span>;</span><br><span class="line">    <span class="attribute">font-size</span>: <span class="number">1.6rem</span>;</span><br><span class="line">    <span class="attribute">line-height</span>: <span class="number">2.7rem</span>;</span><br><span class="line">    <span class="attribute">text-align</span>: center;</span><br><span class="line">    <span class="attribute">border-radius</span>: <span class="number">100%</span>;</span><br><span class="line">    <span class="attribute">background-color</span>: <span class="number">#ffffff</span>;</span><br><span class="line">  &#125;</span><br><span class="line">&lt;/style&gt;</span><br></pre></td></tr></table></figure>

<p><a href="https://wgjh5.github.io/vueSummarize/vuejs%20%E7%A7%BB%E5%8A%A8%E7%AB%AF%20%E5%AE%9E%E7%8E%B0div%E6%8B%96%E6%8B%BD%E7%A7%BB%E5%8A%A8/app/dist/index.html#/app">demo</a></p>

      
    </div>
    <footer class="article-footer">
      <a data-url="wgjh5.github.io/2019/08/07/vuejs 移动端 实现div拖拽移动/" data-id="cjzdy95um000vacu8f4z92971"
         class="article-share-link">分享</a>
      
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/vue/">vue</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/vue-移动端-实现div拖拽移动/">vue 移动端 实现div拖拽移动</a></li></ul>

    </footer>

  </div>

  
    
  <nav class="article-nav">
    
      <a href="/2019/08/07/vue-pdf简单用法/" class="article-nav-link">
        <strong class="article-nav-caption">前一篇</strong>
        <div class="article-nav-title">
          
            vue-pdf
          
        </div>
      </a>
    
    
      <a href="/2019/08/07/vue-i18n用法/" class="article-nav-link">
        <strong class="article-nav-caption">后一篇</strong>
        <div class="article-nav-title">vue-i18n</div>
      </a>
    
  </nav>


  

  
    
    <div class="gitalk" id="gitalk-container"></div>
    <link rel="stylesheet" href="https://unpkg.com/gitalk/dist/gitalk.css">
        <script src="https://unpkg.com/gitalk/dist/gitalk.min.js"></script>
            <script src="https://cdn.bootcss.com/blueimp-md5/2.10.0/js/md5.min.js"></script>
                <script type="text/javascript">
                    var gitalk = new Gitalk({
                        clientID: '64ce5333a42f02cf200d',
                        clientSecret: '7d37a3d8d3c9e215de06d3078bd1894053368739',
                        repo: 'wgjh5.github.io',
                        owner: 'wgjh5',
                        admin: ['wgjh5'],
                        // id: location.pathname,      // Ensure uniqueness and length less than 50
                        id: md5(location.pathname),
                        distractionFreeMode: true, // Facebook-like distraction free mode
                        pagerDirection: 'last'
                    })

                    gitalk.render('gitalk-container')
                </script>
                
  

</article>



</section>
                <footer class="footer">
  <div class="outer">
    <div class="float-right">
      <ul class="list-inline">
  
    <li><i class="fe fe-smile-alt"></i> <span id="busuanzi_value_site_uv"></span></li>
  
</ul>
    </div>
    <ul class="list-inline">
      <li>&copy; 2019 一只正在成长的程序猿</li>
      <li>Powered by <a href="http://hexo.io/" target="_blank">Hexo</a></li>
      <li>Theme  <a href="https://github.com/zhwangart/hexo-theme-ocean">Ocean</a></li>
    </ul>
  </div>
</footer>

        </main>
        <aside class="sidebar">
            <button class="navbar-toggle"></button>

<nav class="navbar">
  
    <div class="logo">
      <a href="/"><img src="/images/shark.svg" alt="一只正在成长的程序猿"></a>
    </div>
  
  <ul class="nav nav-main">
    
      <li class="nav-item">
        <a class="nav-item-link" href="/">主页</a>
      </li>
    
      <li class="nav-item">
        <a class="nav-item-link" href="/archives">归档</a>
      </li>
    
      <li class="nav-item">
        <a class="nav-item-link" href="/about">关于</a>
      </li>
    
    <li class="nav-item">
      <a class="nav-item-link nav-item-search" title="搜索">
        <i class="fe fe-search"></i>
        搜索
      </a>
    </li>
  </ul>
</nav>

<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      <div class="totop" id="totop">
  <i class="fe fe-rocket"></i>
</div>
    </li>
    <li class="nav-item">
      
        <a class="nav-item-link" target="_blank" href="/atom.xml" title="RSS Feed">
          <i class="fe fe-feed"></i>
        </a>
      
    </li>
  </ul>
</nav>

<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
        </aside>
        <script src="/js/jquery-2.0.3.min.js"></script>
<script src="/js/lazyload.min.js"></script>
<script src="/js/snap.svg-min.js"></script>
<script src="/js/busuanzi-2.3.pure.min.js"></script>


  <script src="/fancybox/jquery.fancybox.min.js"></script>



  <script src="/js/search.js"></script>


<script src="/js/ocean.js"></script>

    </body>

    </html>